package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    //Controller需要注入Service层，才可以调用业务层的方法！
    @Autowired
    UserService userService;//注入的是接口
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录：参数={}",userLoginParam);
        //调用业务层的login方法
        //Ctrl去接口 Ctrl+Alt去接口实现类 Ctrl+Shift去方法返回值类
        UserVO userVO = userService.login(userLoginParam);
        //将业务层返回的userVO对象封装到JsonResult中返回给前端
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户，参数为={}",userSaveParam);
        userService.userSave(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户，参数：{}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("userId = "+userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("userId = "+userId+"status = "+status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户，参数={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable Long parentId){
        log.debug("查询审批人，参数={}",parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }
}
